package node;

import lexer.LexType;
import parser.Parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class VarDeclNode extends ASTNode {
    public final String name = "<VarDecl>";
    // VarDecl → BType VarDef { ',' VarDef } ';'
    private final BTypeNode bTypeNode;
    private List<VarDefNode> varDefNodes = new ArrayList<>();

    public VarDeclNode(BTypeNode bTypeNode, List<VarDefNode> varDefNodes) {
        this.bTypeNode = bTypeNode;
        this.varDefNodes = varDefNodes;
    }

    @Override
    public void show() throws IOException {
        bTypeNode.show();
        for (int i = 0; i < varDefNodes.size(); i++) {
            varDefNodes.get(i).show();
            if (i != varDefNodes.size() - 1) {
                Parser.parserWriter.write(LexType.COMMA + " " + "," + "\n");
            }
        }
        Parser.parserWriter.write(LexType.SEMICN + " " + ";" + "\n");
        Parser.parserWriter.write(name + "\n");
    }

    public BTypeNode getbTypeNode() {
        return bTypeNode;
    }

    public List<VarDefNode> getVarDefNodes() {
        return varDefNodes;
    }
}
